package com.singleton;

import java.util.HashMap;
import java.util.Map;

public class Singleton6 {
    private static Map<String,Singleton6> map = new HashMap<String,Singleton6>();
    static{
        Singleton6 single = new Singleton6();
        map.put(single.getClass().getName(), single);
    }
    //保护的默认构造子
    protected Singleton6(){}
    //静态工厂方法,返还此类惟一的实例
    public static Singleton6 getInstance(String name) {
        if(name == null) {
            name = Singleton6.class.getName();
        }
        if(map.get(name) == null) {
            try {
                map.put(name, (Singleton6) Class.forName(name).newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return map.get(name);
    }
}

class testSingleton6{
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            final int a=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Singleton6.getInstance(null).hashCode());
                }
            }).start();
        }
    }
}